package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.db.dao.LitemallShopMerchandiseMapper;
import org.linlinjava.litemall.db.dao.LitemallShopMerchandiseMapperEx;
import org.linlinjava.litemall.db.domain.LitemallShopMerchandise;
import org.linlinjava.litemall.db.domain.LitemallShopMerchandiseExample;
import org.linlinjava.litemall.db.domain.LitemallShopMerchandiseVo;
import org.linlinjava.litemall.db.domain.ShopMerchandiseStatisticsVo;
import org.linlinjava.litemall.db.vo.shopmerchandise.InNumStatisticsVO;
import org.springframework.stereotype.Service;

 

@Service
public class LitemallShopMerchandiseService extends MPJBaseServiceImpl<LitemallShopMerchandiseMapper,LitemallShopMerchandise> {
    @Resource
    private LitemallShopMerchandiseMapper litemallShopMerchandiseMapper;
    @Resource
    private LitemallShopMerchandiseMapperEx litemallShopMerchandiseMapperEx;




   /* public void updateById(LitemallShopMerchandise merchandise) {
        merchandise.setUpdateTime(LocalDateTime.now());
        litemallShopMerchandiseMapper.updateByPrimaryKeySelective(merchandise);
    }*/

    public LitemallShopMerchandise queryById(Integer id, Integer shopId) {
        LitemallShopMerchandiseExample merchandise = new LitemallShopMerchandiseExample();
        merchandise.or().andIdEqualTo(id).andLogicalDeleted(false).andShopIdEqualTo(shopId);
        return litemallShopMerchandiseMapper.selectOneByExample(merchandise);
    }

    public void deleteById(Integer id) {
        LitemallShopMerchandiseExample merchandise = new LitemallShopMerchandiseExample();
        merchandise.or().andIdEqualTo(id);
        LitemallShopMerchandise shopMerchandise = new LitemallShopMerchandise();
        shopMerchandise.setDeleted(true);
        shopMerchandise.setUpdateTime(LocalDateTime.now());
        litemallShopMerchandiseMapper.updateByExampleSelective(shopMerchandise, merchandise);
    }

    public int create(LitemallShopMerchandise merchandise) {
        merchandise.setAddTime(LocalDateTime.now());
        merchandise.setUpdateTime(LocalDateTime.now());
        return litemallShopMerchandiseMapper.insertSelective(merchandise);
    }

    public Integer count(Integer shopId) {
        LitemallShopMerchandiseExample merchandise = new LitemallShopMerchandiseExample();
        merchandise.or().andDeletedEqualTo(false).andShopIdEqualTo(shopId);
        return (int)litemallShopMerchandiseMapper.countByExample(merchandise);
    }


    public LitemallShopMerchandise findById(Integer id) {
        return litemallShopMerchandiseMapper.selectByPrimaryKey(id);
    }

    public List<LitemallShopMerchandise> queryByMerIds(Integer shopId, List<Integer> merIds) {
        LitemallShopMerchandiseExample example = new LitemallShopMerchandiseExample();
        example.or().andShopIdEqualTo(shopId).andMerchandiseIdIn(merIds).andDeletedEqualTo(false);
        return litemallShopMerchandiseMapper.selectByExample(example);
    }

    public LitemallShopMerchandise queryByMerIdAndShopId(Integer merId, Integer shopId) {
        return litemallShopMerchandiseMapper.selectOne(Wrappers.lambdaQuery(LitemallShopMerchandise.class)
                .eq(LitemallShopMerchandise::getShopId,shopId).eq(LitemallShopMerchandise::getMerchandiseId,merId));
    }

    public List<LitemallShopMerchandiseVo> queryByMerchandiseSnAndName(Integer shopId, String name,
    		String merchandiseSn,Integer waning, int page, int limit) {
    	//PageHelper.startPage(page, limit);
        return litemallShopMerchandiseMapperEx.selectByMerchandiseSnAndName(shopId, name, merchandiseSn,waning);
    }

    public List<ShopMerchandiseStatisticsVo> statistics(Integer shopId, String merchandiseName,
    		String merchandiseSn, String beginTime, String endTime, int page, int limit) {
    	//PageHelper.startPage(page, limit);
    	return litemallShopMerchandiseMapperEx.statistics(shopId, merchandiseName, merchandiseSn, beginTime, endTime);
    }

}
